package com.mars.push.core.ctrl;

import com.mars.fw.web.reponse.King;
import com.mars.fw.web.reponse.KingCode;
import com.mars.push.api.common.DeviceType;
import com.mars.push.api.common.NoticeType;
import com.mars.push.api.common.PushMessageType;
import com.mars.push.api.common.PushType;
import com.mars.push.api.dto.ManagePushMessageInput;
import com.mars.push.api.dto.PushMessageInput;
import com.mars.push.core.service.PushServiceImpl;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author King
 * @create 2020/5/25 14:10
 */
@Api(tags = "【VV Work】 推送接口")
@RestController
@RequestMapping(value = "/api/v1/push")
public class VvPushServerController {

    @Autowired
    private PushServiceImpl pushService;


    /**
     * 消息推送
     *
     * @param input
     * @return
     */
    @ApiOperation(value = "推送接口")
    @PostMapping(value = "/noticePush")
    public King noticePush(@RequestBody PushMessageInput input) {
        return King.success(KingCode.SUCCESS,pushService.push(input));
    }


    /**
     * 消息推送测试
     *
     * @return
     */
    @ApiOperation(value = "推送测试接口")
    @PostMapping(value = "/testPush")
    public King noticePush(@RequestBody ManagePushMessageInput input) {
        return King.success(KingCode.SUCCESS,pushService.push(create(input)));
    }

    private PushMessageInput create(ManagePushMessageInput input) {
        PushMessageInput messageInput = new PushMessageInput();
        messageInput.setAlert(creatStr())
                .setDeviceType(DeviceType.ALL)
                .setNoticeType(NoticeType.REGISTER_ID)
                .setPushType(PushType.J_PUSH)
                .setPushMessageType(PushMessageType.ALL)
                .setCustomerMessage(creatStr())
                .setTargets(Lists.newArrayList(input.getTargets()));
        return messageInput;
    }


    private String creatStr() {
        //定义一个空字符串
        String result = "";
        //进行6次循环
        for (int i = 0; i < 6; i++) {
            //生成一个97-122之间的int类型整数
            int intVal = (int) (Math.random() * 26 + 97);
            //将intValue强制转换为char类型后连接到result后面
            result = result + (char) intVal;
        }
        return result;
    }

}
